// get schema.
import axios from 'axios';
import { Actions } from '@jsonforms/core';

axios.defaults.baseURL = 'http://localhost:8000/';

export const getSchema = async (path) => {
  const res = await axios.get(`${path}/schema.json`);
  return res.data;
};
export const getData = async (path) => {
  const res = await axios.get(`${path}/data.json`);
  return res.data;
};
export const getSchemaUI = async (path) => {
  const res = await axios.get(`${path}/schema-ui.json`);
  return res.data;
};

export const init = async () => {
  const model = 'templates/lock';
  try {
    const [schema, data, uischema] = await Promise.all([
      getSchema(model),
      getData(model),
      getSchemaUI(model),
    ]);
    return {
      type: Actions.INIT,
      schema,
      data,
      uischema,
    };
  } catch (error) {
    return {
      type: Actions.INIT,
      schema: {},
      data: {},
      uischema: {},
    };
  }
};
